<template>
  <div>
    <BasicTable @register="registerTable">
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <Drawer @register="registerDrawer" @success="handleSuccess"></Drawer>
  </div>
</template>
<script lang="ts" setup name="addCheckRecord">
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/Table';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import Drawer from './drawer.vue';
import { columns, searchFormSchema } from './data';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import { Api, check } from './api';

const [registerDrawer, { openDrawer }] = useDrawer();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'addcheckRecord-template',
  tableProps: {
    //表格标题
    title: '核销补录',
    //主键字段
    rowKey: 'addcheckRecordId',
    //列表接口
    listUrl: Api.page,
    handlerName: 'busAddCheckRecordListHandler',
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    //默认排序(排序字段上需设置defaultSortOrder:'descend')
    defSort: {
      order: 'createTime',
      sort: 'desc',
    },
    //操作列配置
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  },
});
const { createMessage, createConfirm } = useMessage();
const [registerTable, { reload }, { rowSelection, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '通过',
      onClick: handlerCheck.bind(null, record, 1),
      ifShow: _action => {
        return record.isCheck == 0;
      },
    },
    {
      label: '拒绝',
      onClick: handlerCheck.bind(null, record, 2),
      ifShow: _action => {
        return record.isCheck == 0;
      },
    },
  ];
}

/**
 * 通过
 */
function handlerCheck(record, isCheck) {
  let addcheckRecordId = record.addcheckRecordId;
  let desc = isCheck == 1 ? '通过' : '拒绝';
  createConfirm({
    iconType: 'info',
    title: '补录确认',
    content: '确认' + desc + '补录确认？',
    onOk: async () => {
      await check({ addcheckRecordId: addcheckRecordId, isCheck: isCheck });
      handleSuccess(true, {});
    },
    onCancel: async () => {},
  });
}
</script>
